package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.shop.bo.GoodsInfoBO;
import com.junzhiit.nengji.shop.dao.TypeDAO;
import com.junzhiit.nengji.shop.model.Type;
import com.junzhiit.nengji.shop.model.enums.GoodsStatus;
import com.junzhiit.nengji.shop.model.enums.Is;
import com.junzhiit.nengji.shop.model.enums.OrderType;
import com.junzhiit.nengji.shop.model.enums.TypeLevel;

@Repository
public class TypeDAOImpl extends AbstractBaseDAO<Type> implements TypeDAO {

	@Override
	public List<Type> findAll(String shopId) {
		String sql = "select * from `type` where is_delete=? and shop_id=? order by sort";

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(shopId);

		return super.queryForList(sql, Type.class, param);
	}

	@Override
	public List<Type> getAllName(String shopId) {
		String sql = "select id,full_name from `type` where is_delete=? and level<>? and shop_id=? order by level asc";

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setInt(TypeLevel.THREE.toIntValue());
		param.setString(shopId);
		return super.queryForList(sql, Type.class, param);

	}

	@Override
	public List<Type> getGoodsType(String shopId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select id,full_name,name from `type` where is_delete=?");

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNoneBlank(shopId)){
			sql.append(" and shop_id=?");
			param.setString(shopId);
		}
		
		sql.append(" order by full_code asc ");
		
		return super.queryForList(sql.toString(), Type.class, param);
	}

	@Override
	public int getTypeExist(Type type) {

		StatementParameter param = new StatementParameter();

		StringBuffer sql = new StringBuffer();
		sql.append("select count(*) from `type` where is_delete=? and shop_id=?");
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(type.getShopId());

		if (StringUtils.isNotBlank(type.getName())) {
			sql.append(" and name=?");
			param.setString(type.getName());
		}

		if (StringUtils.isNotBlank(type.getParentId())) {
			sql.append(" and parent_id=?");
			param.setString(type.getParentId());
		}
		
		return super.queryForInt(sql.toString(), param);
	}

	@Override
	public String getMaxCode(String parentId) {
		StatementParameter param = new StatementParameter();
		StringBuffer sql = new StringBuffer();
		sql.append("select max(code) from type where 1 = 1");
		if (StringUtils.isNotBlank(parentId)) {
			sql.append(" and parent_id = ?");
			param.setString(parentId);
		}
		return super.queryForString(sql.toString(), param);
	}

	@Override
	public String getMaxFullCode(String parentId) {
		StatementParameter param = new StatementParameter();
		StringBuffer sql = new StringBuffer();
		sql.append("select max(full_code) from type where 1 = 1");
		if (StringUtils.isNotBlank(parentId)) {
			sql.append(" and parent_id = ?");
			param.setString(parentId);
		}
		return super.queryForString(sql.toString(), param);
	}

	@Override
	public int getMaxSort(String parentId) {
		StatementParameter param = new StatementParameter();
		StringBuffer sql = new StringBuffer();
		sql.append("select ifnull(max(sort),0)  from type where 1 = 1");
		if (StringUtils.isNotBlank(parentId)) {
			sql.append(" and parent_id = ?");
			param.setString(parentId);
		}
		return super.queryForInt(sql.toString(), param);
	}

	@Override
	public List<Type> getChildId(String parentId) {
		String sql = "select id from type where parent_id=?";
		StatementParameter param = new StatementParameter();
		param.setString(parentId);
		return super.queryForList(sql, Type.class, param);
	}

	@Override
	public List<Type> getChild(String parentId) {
		String sql = "select * from type where parent_id=?";
		StatementParameter param = new StatementParameter();
		param.setString(parentId);
		return super.queryForList(sql, Type.class, param);
	}

	@Override
	public List<Type> getGoodsAuditType(String shopId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select distinct t.* from `type` t inner join goods g on g.type_id=t.id");
		sql.append(" and g.status =? and t.is_delete=? and g.is_delete=?");
		
		StatementParameter param = new StatementParameter();
		param.setInt(GoodsStatus.AUDIT.toIntStatus());
		param.setInt(IsDelete.NO.toIntValue());
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNoneBlank(shopId)){
			sql.append(" and t.shop_id=?");
			param.setString(shopId);
		}
		
		sql.append(" order by t.full_code asc ");
		return super.queryForList(sql.toString(), Type.class, param);
	}
	
	
	@Override
	public Type getType(Type type){
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from type");
		sql.append(" where is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(type.getShopId())){
			sql.append(" and shop_id=?");
			param.setString(type.getShopId());
		}
		
		if(StringUtils.isNotBlank(type.getName())){
			sql.append(" and name=?");
			param.setString(type.getName());
		}
		
		if (StringUtils.isNotBlank(type.getParentId())) {
			sql.append(" and parent_id = ?");
			param.setString(type.getParentId());
		}
		
		if(type.getLevel()!=null){
			sql.append(" and level=?");
			param.setInt(type.getLevel());
		}
		
		return super.query(sql.toString(), Type.class, param);
	}

	@Override
	public List<GoodsInfoBO> getGoodsByType(String typeId) {
		StatementParameter param = new StatementParameter();
		StringBuffer sql = new StringBuffer();
		sql.append(" select g.id,g.shop_id,g.code,g.name,g.hint,g.type_id,g.brand_id,g.reference_price,g.stock ");
		sql.append(" ,g.is_open_sku,g.is_free_shipping,g.is_open_rebate,g.is_open_member,g.type,g.is_open_sell,g.discount ");
		sql.append(" ,g.status,g.audit_fail_description,g.is_recommend,g.sales_volume,g.postage_type,g.postage_price,g.postage_template_id ");
		sql.append(" ,g.postage_weight,g.profit,g.is_synchronous,g.is_help_peasant,g.is_chain_import ");
		sql.append(" ,s.`name` shopName,s.shop_head_img_url shopHeadImgUrl,s.type shopType,s.mobile_phone mobilePhone,s.address address ");
		sql.append(" ,(select gi.file_name from goods_images gi where gi.goods_id=g.id and gi.type='cover' limit 0,1)fileName ");
		sql.append(" ,(case g.is_open_sku when ? then g.price when ? then (select min(sc.price)");
		sql.append(" from sku_content sc where sc.is_delete = ? and sc.goods_id = g.id)end) price ");
		sql.append(" from goods g ");
		sql.append(" left join shop s on g.shop_id = s.id ");
		sql.append(" where g.type_id = ? and g.status = ? and g.type !=? and g.is_delete = ? ");
		
		param.setInt(Is.NO.toIntValue());
		param.setInt(Is.YES.toIntValue());
		param.setInt(IsDelete.NO.toIntValue());
		
		param.setString(typeId);
		param.setInt(GoodsStatus.ACTIVE.toIntStatus());
		param.setInt(OrderType.EAT.toIntValue());
		param.setInt(IsDelete.NO.toIntValue());
		return super.queryForList(sql.toString(), GoodsInfoBO.class, param);
	}
	
	@Override
	public List<GoodsInfoBO> getEatGoodsByType(String typeId,Integer type) {
		StatementParameter param = new StatementParameter();
		StringBuffer sql = new StringBuffer();
		sql.append(" select g.id,g.shop_id,g.code,g.name,g.hint,g.type_id,g.brand_id,g.reference_price,g.stock ");
		sql.append(" ,g.is_open_sku,g.is_free_shipping,g.is_open_rebate,g.is_open_member,g.type,g.is_open_sell,g.discount ");
		sql.append(" ,g.status,g.audit_fail_description,g.is_recommend,g.sales_volume,g.postage_type,g.postage_price,g.postage_template_id ");
		sql.append(" ,g.postage_weight,g.profit,g.is_synchronous,g.is_help_peasant,g.is_chain_import ");
		sql.append(" ,s.`name` shopName,s.shop_head_img_url shopHeadImgUrl,s.type shopType,s.mobile_phone mobilePhone,s.address address ");
		sql.append(" ,(select gi.file_name from goods_images gi where gi.goods_id=g.id and gi.type='cover' limit 0,1)fileName ");
		sql.append(" ,(case g.is_open_sku when ? then g.price when ? then (select min(sc.price)");
		sql.append(" from sku_content sc where sc.is_delete = ? and sc.goods_id = g.id)end) price ");
		sql.append(" from goods g ");
		sql.append(" left join shop s on g.shop_id = s.id ");
		sql.append(" where g.type_id = ? and g.status = ? and g.is_delete = ?  ");
		
		param.setInt(Is.NO.toIntValue());
		param.setInt(Is.YES.toIntValue());
		param.setInt(IsDelete.NO.toIntValue());
		
		param.setString(typeId);
		param.setInt(GoodsStatus.ACTIVE.toIntStatus());
		param.setInt(IsDelete.NO.toIntValue());
		if(type!=null){
			sql.append(" and g.type =? ");
			param.setInt(type);
		}
		return super.queryForList(sql.toString(), GoodsInfoBO.class, param);
	}
	
}
